"""
工作日志页面类
"""
from selenium.webdriver.common.by import By
from page.base_page import BasePage
from time import sleep


class WorkLogPage(BasePage):
    # 工作日志相关页面路径
    url = "/index.php?m=log&a=index"

    # 定位器
    more_menu_locator = (By.CSS_SELECTOR,
                         "body > div.navbar.navbar-inverse.navbar-fixed-top > div > div > div.nav-collapse.collapse > ul:nth-child(1) > li > a")
    log_menu_locator = (By.CSS_SELECTOR,
                        "body > div.navbar.navbar-inverse.navbar-fixed-top > div > div > div.nav-collapse.collapse > ul:nth-child(1) > li > ul > li:nth-child(1) > a")
    create_log_btn_locator = (By.CSS_SELECTOR, "body > div.container > div.row > div:nth-child(2) > div > a")
    submit_btn_locator = (By.NAME, "submit")
    table_locator = (By.CSS_SELECTOR, "body > div.container > div.row > div > form > table > tbody")
    radio_btn_locator = (By.CSS_SELECTOR,
                         "body > div.container > div.row > div > form > table > tbody > tr:nth-child(3) > td:nth-child(2) > input[type=radio]:nth-child(3)")
    subject_input_locator = (By.ID, "subject")
    log_type_locator = (By.XPATH, "/html/body/div[5]/div[2]/div/form/table/tbody/tr[3]/td[2]/input[1]")
    content_frame_locator = (By.CSS_SELECTOR, "iframe")  # 富文本框的iframe
    content_input_locator = (By.CSS_SELECTOR, "body")  # 富文本框内的body元素
    primary_btn_locator = (By.NAME, "btn_primary")

    def click_more_menu(self):
        """点击更多菜单"""
        self.click(self.more_menu_locator)
        sleep(2)

    def click_log_menu(self):
        """点击日志菜单"""
        self.click(self.log_menu_locator)
        sleep(2)

    def click_create_log(self):
        """点击创建日志按钮"""
        self.click(self.create_log_btn_locator)
        sleep(2)

    def click_submit(self):
        """点击提交按钮"""
        self.click(self.submit_btn_locator)
        sleep(2)

    def select_radio_button(self):
        """选择指定的单选按钮"""
        # 获取表格元素列表并选择第三个表格中的单选按钮
        tables = self.driver.find_elements(*self.table_locator)
        if len(tables) > 2:
            tables[2].find_element(*self.radio_btn_locator).click()
            sleep(2)

    def input_subject(self, subject):
        """输入日志标题"""
        self.send_keys(self.subject_input_locator, subject)
        sleep(2)

    def click_log_type(self):
        """点击日志类型"""
        self.click(self.log_type_locator)
        sleep(2)

    def input_content(self, content):
        """在富文本框中输入日志内容"""
        # 切换到iframe
        self.driver.switch_to.frame(self.find_element(self.content_frame_locator))
        # 在富文本框中输入内容
        self.find_element(self.content_input_locator).send_keys(content)
        sleep(2)
        # 切换回主文档
        self.driver.switch_to.default_content()

    def click_primary_button(self):
        """点击按钮（保存）"""
        self.click(self.primary_btn_locator)
        sleep(2)

    def create_work_log(self, subject, content):
        """创建工作日志的完整流程"""
        self.click_more_menu()
        self.click_log_menu()
        self.click_create_log()
        self.click_submit()
        self.select_radio_button()
        self.input_subject(subject)
        self.click_log_type()
        self.input_content(content)
        self.click_primary_button()
